package org.joy.db2;

import java.util.ArrayList;
import java.util.List;

public class SearchEntry implements Comparable<SearchEntry> {

    private int URLID;
    private double score;
    private List<Integer[]> pos;
    private int prox;

    public SearchEntry(Integer URLID, MergedDocHits hits) {
	this.URLID = URLID;
	pos = hits.getPos();
	prox = Proximity.getProximity(pos);
	score = prox + hits.getScore();
    }

    public double getScore() {
	return score;
    }

    private List<Integer[]> getInDocumentPos() {
	ArrayList<Integer[]> ret = new ArrayList<Integer[]>();

	for (Integer[] a : pos) {
	    ArrayList<Integer> lst = new ArrayList<Integer>();
	    for (Integer i : a) {
		if (i >= 0) {
		    lst.add(i);
		}
	    }
	    ret.add(lst.toArray(new Integer[0]));
	}
	return ret;
    }

    public Integer[] getSnippetPos() {
	return Proximity.getMinRangeInText(getInDocumentPos());
    }

    public int getURLID() {
	return URLID;
    }

    public int getProx() {
	return prox;
    }

    @Override
    public String toString() {
	return "SearchEntry [URLID=" + URLID + ", pos=" + pos + ", prox="
		+ prox + ", score=" + score + "]";
    }

    @Override
    public int compareTo(SearchEntry o) {
	return Double.compare(score, o.score);
    }
}
